//
//  RefreshTableView.m
//  ifangchou
//
//  Created by mac on 15/11/25.
//  Copyright © 2015年 ifangchou. All rights reserved.
//

#import "RefreshTableView.h"
#import "MJRefresh.h"

@interface RefreshTableView()

@property(nonatomic,assign) BOOL isRefresh;
@property(nonatomic,assign) BOOL isLoadMore;

@end

@implementation RefreshTableView

#pragma mark - Public

- (void)setupRefresh
{
    // 1.下拉刷新(进入刷新状态就会调用self的headerRereshing)
    [self addHeaderWithTarget:self action:@selector(headerRereshing)];
    // 自动刷新(一进入程序就下拉刷新)
    //[self.luckyMoneyTableView headerBeginRefreshing];
    
    // 2.上拉加载更多(进入刷新状态就会调用self的footerRereshing)
    [self addFooterWithTarget:self action:@selector(footerRereshing)];
}


- (void)reloadTableViewData
{
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
       
        dispatch_async(dispatch_get_main_queue(), ^{
           [self reloadData];
           [self headerEndRefreshing];
           [self footerEndRefreshing];

        });
        
    });
}

- (void)stopRefresh
{
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        
        dispatch_async(dispatch_get_main_queue(), ^{
            [self headerEndRefreshing];
            [self footerEndRefreshing];
        });
        
    });
}

#pragma mark -

- (void)headerRereshing
{
    self.isRefresh = YES;
    self.isLoadMore = NO;
    
    if (self.refreshDelegate != nil && [self.refreshDelegate respondsToSelector:@selector(tableView:didListViewRefreshOrLoadMoreData:)]) {
        [self.refreshDelegate tableView:self didListViewRefreshOrLoadMoreData:self.isRefresh];
    }
}

- (void)footerRereshing
{
    self.isRefresh = NO;
    self.isLoadMore = YES;
    
    if (self.refreshDelegate != nil && [self.refreshDelegate respondsToSelector:@selector(tableView:didListViewRefreshOrLoadMoreData:)]) {
        [self.refreshDelegate tableView:self didListViewRefreshOrLoadMoreData:self.isRefresh];
    }
}


@end
